import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.RecursiveTask;

class Test {
    public int val = 0;
    public String name = "";

    public Test() {
        val = 0;
        name = "";
    }

    public Test(int val, String name) {
        this.val = val;
        this.name = name;
    }

    public void ToString() {
        System.out.println("Test [val: " + val + ", name: " + name + "]");
    }
}

class RecvServerMessge implements TcpClient.Message {

    @Override
    public void onRecvMessage(String message) {
        if (message != null) {
            System.out.println("recv server message len: " + message.length() + ", content: " + message);
        }
        return;
    }
}

class RecvClientMessage implements TcpServer.Message {

    @Override
    public void onRecvMessage(TcpServer server, Socket sock, String message) {
        if (server != null && sock != null && message != null) {
            System.out.println("recv client [" + sock.getRemoteSocketAddress() + "] message: " + message);
            server.writeStringMessage(sock, "Hello, welcome! I have received your message, thx.");
        }
        return;
    }
}

public class Main {

    public static String getCurrentFormatDateTime() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.zzz");
        return dateFormat.format(date);
    }

    public static void testTcpClient() throws InterruptedException {
        RecvServerMessge recvMsg = new RecvServerMessge();
        TcpClient client = new TcpClient();
        client.setMessageCallback(recvMsg);

        /*client.connect("172.18.56.205", 60000);

        for (int i = 0; i < 30; ++i) {
            Thread.sleep(1000);
        }

        client.disconnect();
        Thread.sleep(5000);*/

        client.connect("172.18.56.205", 60000);

        for (int i = 0; i < 3000; ++i) {
            client.writeStringMessage("[" + getCurrentFormatDateTime() + "] " + ", Hello, server!!!");
            Thread.sleep(3000);
        }

        client.disconnect();
        return;
    }

    public static void testTcpServer() throws InterruptedException {
        RecvClientMessage recvMsg = new RecvClientMessage();
        TcpServer server = new TcpServer();
        server.setMessageCallback(recvMsg);

        server.quickStart(56666);
        //server.start("127.0.0.1", 56666, 50);
        for (int i = 0; i < 3000; ++i) {
            Thread.sleep(5000);
        }
        server.stop();

        return;
    }

    public static void main(String[] args) {
        System.out.println("Hello and welcome!");

        for (int i = 1; i <= 5; ++i) {
            System.out.println("i = " + i);
        }

        Test test = new Test(123, "xiao ming");
        //test.val =  123;
        //est.name = "xiao ming";
        //est.name = "xiao ming";
        test.ToString();

        try {
            //testTcpClient();
            testTcpServer();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return;
    }
}